<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>PHP lexical structure</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="PHP, learn PHP, lexical structure, programming, language">
<meta name="description" content="In this part of the PHP tutorial, we cover PHP lexical structure.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>PHP lexical structure</h1>

<p>
Computer languages, like human languages, have a lexical structure. A source 
code of a PHP script consists of tokens. Tokens are atomic code elements. 
In PHP language, we have comments, variables, literals, operators, delimiters and keywords.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<h2>Comments</h2>

<p>
<b>Comments</b> are used by humans to clarify the source code. All comments in PHP follow
the <b>#</b> character. 
</p>

<pre class="code">
&lt;?php

# comments.php
# author Jan Bodnar
# ZetCode 2009

echo "This is comments.php script\n";

?&gt;
</pre>

<p>
Everything that follows the # character is ignored by the PHP interpreter. 
</p>

<pre class="code">
// comments.php
// author Jan Bodnar
// ZetCode 2009

/*
 comments.php
 author Jan Bodnar
 ZetCode 2009
*/
</pre>

<p>
PHP also recognizes the comments from the C language. 
</p>


<h2>White space</h2>

<p>
White space in PHP is used to separate tokens in PHP source file. It is used to improve
readability of the source code. 
</p>

<p>
public $isRunning;
</p>

<p>
White spaces are required in some places. For example between the access specifier
and the variable name. In other places, it is forbidden. It cannot be present in
variable identifiers. 
</p>

<pre>
$a=1;
$b = 2;
$c  =  3;
</pre>

<p>
The amount of space put between tokens is irrelevant for the PHP interpreter. 
</p>

<pre>
$a = 1;
$b = 2; $c = 3;
$d 
  = 
   4;
</pre>

<p>
We can put two statements into one line. Or one statement into three lines. However, source 
code should be readable for humans. There are accepted standards of
how to lay out your source code. 
</p>


<h2>Semicolon</h2>

<p>
A semicolon is used to mark the end of a statement in PHP. It is mandatory. 
</p>

<pre>
$a = 34;
$b = $a * 34 - 34;
echo $a;
</pre>

<p>
Here we have three different PHP statements. The first is an assignment. It puts 
a value into the $a variable. The second one is an expression. The expression is 
evaluated and the output is given to the $b variable. The third one is a command. 
It prints the $a variable.
</p>


<h2>Variables</h2>

<p>
A <code>variable</code> is an identifier, which holds a value. 
In programming we say, that we assign a value to 
a variable. Technically speaking, a variable is a reference to a computer 
memory, where the value is stored. 
In PHP language, a variable can hold a string, a number or various objects 
like a function or a class. Variables can be assigned 
different values over time. 
</p>

<p>
Variables in PHP consist of the $ character and a label. A label can be 
created from alphanumeric characters and an underscore (_) character. A variable cannot begin
with a number. The PHP interpreter can then distinguish between a number 
and a variable more easily. 
</p>

<pre>
$Value
$value2
$company_name
</pre>

<p>
These were valid identifiers. 
</p>

<pre>
$12Val
$exx$
$first-name
</pre>

<p>
These were examples of invalid identifiers. 
</p>


<p>
The variables are <b>case sensitive</b>. This means, that  
<code>$Price</code>, <code>$price</code>, and <code>$PRICE</code>
are three different identifiers. 
</p>

<pre class="code">
&lt;?php

$number = 10;
$Number = 11;
$NUMBER = 12;

echo $number, $Number, $NUMBER;

echo "\n";

?&gt;
</pre>

<p>
In our script, we assign three numeric values to three variables and print them. 
</p>

<pre>
101112
</pre>

<p>
This is the output of the script. 
</p>


<h2>A literal</h2>

<p>
A <code>literal</code> is any notation for representing a 
value within the PHP source code. Technically, a literal will be assigned a value 
at compile time, while a variable  will be assigned at runtime.
</p>

<pre>
$age = 29;
$nationality = "Hungarian";
</pre>

<p>
Here we assign two literals to variables. Number 29 and string Hungarian are literals. 
</p>

<pre class="code">
&lt;?php

$name1 = "Jane ";
$age1 = 17;

$name2 = "Rose ";
$age2 = 16;

echo "Patrick 34\n";
echo "Luke 22\n";

echo $name1, $age1, "\n";
echo $name2, $age2, "\n";

?&gt;
</pre>

<p>
If we do not assign a literal to a variable, there is no way, how we can 
work with it. It is dropped. 
</p>

<pre>
$ php literals.php
Patrick 34
Luke 22
Jane 17
Rose 16
</pre>

<p>
This is the output of the literals.php script. 
</p>


<h2>Operators</h2>

<p>
An <code>operator</code> is a symbol used to perform an action 
on some value. (answers.com) 
</p>

<pre>
+    -    *    /    %    ++    --
=    +=   -=   *=   /=   .=    %=
==   !=   &gt;&lt;   &gt;   &lt;   &gt;=   &lt;= 
&amp;&amp;   ||   !   xor   or    
&amp;    ^    |   ~   .   &lt;&lt;   &gt;&gt;   
</pre>

<p>
These are PHP operators. We will talk about operators later in the tutorial.
</p>


<h2>Delimiters</h2>

<p>
A <code>delimiter</code> is a sequence of one or more characters used 
to specify the boundary between separate, independent regions in plain 
text or other data stream. (wikipedia)
</p>

<pre>
$a = "PHP";
$b = 'Java';
</pre>

<p>
The single and double characters are used to mark the beginning and the end of a string. 
</p>


<pre>
function setDate($date) {
    $this->date = $data;
}

if ( $a > $b) {
    echo "\$a is bigger than \$b";
}

</pre>

<p>
Parentheses are used to mark the function signature. The signature is the function parameters.
Curly brackets are used to mark the beginning and the end of the function body. They are also used
in flow control. 
</p>

<pre>
$a = array(1, 2, 3);
echo $a[1];
</pre>

<p>
The square brackets are used to mark the array index. 
</p>

<pre>
/*
 Author Jan Bodnar
 December 2009
 ZetCode
*/
</pre>


<p>
/* */ delimiters are used to provide C style comments in PHP. 
</p>

<pre>
&lt;?php
// PHP code
?&gt;
</pre>



<p>
The &lt;?php and ?&gt; delimiters are used to delimit PHP code in a file. 
</p>


<h2>Keywords</h2>

<p>
A keyword is a reserved word in the PHP programming language. Keywords are used to 
perform a specific task in the computer program. For example, print a value, do 
repetitive tasks or perform logical operations. 
A programmer cannot use a keyword as an ordinary variable. 
</p>

<p>
The following is a list of PHP keywords.
</p>

<pre>
abstract    and          array()     as          break
case        catch        class       clone       const  
continue    declare      default     do          else
elseif      enddeclare   endfor      endforeach  endif
endswitch   endwhile     extends     final       for 
foreach     function     global      goto        if  
implements  interface    instanceof  namespace   new   
or          private      protected   public      static
switch      throw        try         use         var 
while       xor
</pre>



<p>
The following is a list of PHP compile time constants. 
</p>

<pre>
__CLASS__    __DIR__       __FILE__    __FUNCTION__ 
__METHOD__   __NAMESPACE__ 
</pre>

<p>
Next we have other language constructs.
</p>

<pre>
die()           echo()          empty()     exit()      eval()
include()       include_once()  isset()     list()      require()
require_once()  return()        print()     unset()     
</pre>


<p>
In this part of the PHP tutorial, we covered the basic lexis for the PHP
language. 
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 17, 2009  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

